package com.example.pucha_xian.common;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

public class ColView {
    private static ArrayList<EditText> listText=new ArrayList<>();
    private static ArrayList<Spinner> listSpinner=new ArrayList<>();
    private static ArrayList<ImageView> listView=new ArrayList<>();

    private static void getViewAble(ViewGroup viewgroup)
    {
        List<ViewGroup> group = new ArrayList<>();
        group.add(viewgroup);
        while(!group.isEmpty()){
            ViewGroup viewGroup = group.remove(0);
            if (viewGroup == null)
            { return; }
            int count = viewGroup.getChildCount();
            for (int i = 0; i < count; i++)
            {
                View view = viewGroup.getChildAt(i);
                if (view instanceof EditText)
                {
                    // 若是Button记录下
                    EditText newText = (EditText) view;
                    listText.add(newText);
                }else if (view instanceof Spinner){
                    Spinner newSpi = (Spinner) view;
                    listSpinner.add(newSpi);
                }else if (view instanceof ImageView) {
                    ImageView newImg = (ImageView) view;
                    listView.add(newImg);
                }
                else if (view instanceof ViewGroup) {
                    // 若是布局控件（LinearLayout或RelativeLayout）,继续查询子View
                    //getViewAble(viewGroup);
                    //不要使用递归
                    group.add((ViewGroup)view);
                }
            }
        }
    }

    public static void setDisAble(ViewGroup viewGroup) {
        getViewAble(viewGroup);
        for (EditText editText : listText) {
            editText.setEnabled(false);
        }
        for (Spinner spinner : listSpinner) {
            spinner.setEnabled(false);
        }
        for (ImageView imageView : listView) {
            imageView.setEnabled(false);
        }
    }

    public static void setAble(ViewGroup viewGroup) {
        getViewAble(viewGroup);
        for (EditText editText : listText) {
            editText.setEnabled(true);
        }
        for (Spinner spinner : listSpinner) {
            spinner.setEnabled(true);
        }
        for (ImageView imageView : listView) {
            imageView.setEnabled(true);
        }
    }
}
